import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'dart:math' as math;

class LitPlatformItem extends StatefulWidget {
  final String? platformIcon;
  final String platformName;
  final String? description;
  final String? copyLink;
  final VoidCallback? onTap;
  final VoidCallback? onCopyLink;
  final int recommendFlag; // 新增：推荐标志 0=不推荐 1=推荐
  final String showFlag; // 新增：展示状态 0=不展示 1=展示
  final String? recommendLogo; // 新增：推荐logo链接

  const LitPlatformItem({
    Key? key,
    this.platformIcon,
    required this.platformName,
    this.description,
    this.copyLink,
    this.onTap,
    this.onCopyLink,
    this.recommendFlag = 0, // 默认不推荐
    this.showFlag = '0', // 默认不展示
    this.recommendLogo,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return LitPlatformItemState();
  }
}

class LitPlatformItemState extends State<LitPlatformItem> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: widget.onTap,
      child: Container(
        margin: EdgeInsets.only(
            left: 16.w,
            right: 16.w,
            top: 12.h,    // 增加顶部边距，为推荐图标留空间
            bottom: 8.h
        ),
        child: Stack(
          clipBehavior: Clip.none, // 允许子组件超出边界显示
          children: [
            Container(
              padding: EdgeInsets.all(16.w),
              decoration: BoxDecoration(
                color: Color(0xFF2A2A2A),
                borderRadius: BorderRadius.circular(12.r),
                border: Border.all(
                  color: Color(0xFF3A3A3A),
                  width: 1,
                ),
              ),
              child: Row(
                children: [
                  // 平台图标
                  Container(
                    width: 40.w,
                    height: 40.w,
                    decoration: BoxDecoration(
                      color: Color(0xFF4A4A4A),
                      borderRadius: BorderRadius.circular(8.r),
                    ),
                    child: widget.platformIcon != null
                        ? ClipRRect(
                      borderRadius: BorderRadius.circular(8.r),
                      child: Image.network(
                        widget.platformIcon!,
                        fit: BoxFit.cover,
                        errorBuilder: (context, error, stackTrace) {
                          return Icon(
                            Icons.shopping_bag,
                            color: Colors.grey[400],
                            size: 20.w,
                          );
                        },
                      ),
                    )
                        : Icon(
                      Icons.shopping_bag,
                      color: Colors.grey[400],
                      size: 20.w,
                    ),
                  ),
                  SizedBox(width: 12.w),

                  // 中间内容区域
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        // 平台名称和复制链接按钮
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Flexible(
                              child: Text(
                                widget.platformName,
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 16.sp,
                                  fontWeight: FontWeight.w600,
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                          ],
                        ),

                        // 描述文字
                        if (widget.description != null) ...[
                          SizedBox(height: 4.h),
                          Text(
                            widget.description!,
                            style: TextStyle(
                              color: Color(0xFF9E9E9E),
                              fontSize: 12.sp,
                              fontWeight: FontWeight.normal,
                            ),
                          ),
                        ],
                      ],
                    ),
                  ),

                  // 导航箭头
                  Icon(
                    Icons.chevron_right,
                    color: Color(0xFF9E9E9E),
                    size: 20.w,
                  ),
                ],
              ),
            ),


            if (widget.recommendFlag == 1)
              Positioned(
                top: 0.h, // 向上偏移一些，给旋转后的图片留出空间
                right: -8.w, // 向右偏移一些，确保图片完整显示
                child: Container(
                  width: 80.w,
                  height: 24.w,
                  child: Transform.rotate(
                    angle: math.pi / 6.5, // 顺时针旋转30度
                    child: widget.recommendLogo != null && widget.recommendLogo!.isNotEmpty
                        ? CachedNetworkImage(
                      imageUrl: widget.recommendLogo!,
                      fit: BoxFit.contain,
                      placeholder: (context, url) {
                        debugPrint('正在加载推荐Logo: $url');
                        return Center(
                          child: CircularProgressIndicator(
                            strokeWidth: 2,
                            valueColor: AlwaysStoppedAnimation<Color>(Color(0xFFFF6B35)),
                          ),
                        );
                      },
                      errorWidget: (context, url, error) {
                        debugPrint('推荐Logo加载失败: $url');
                        debugPrint('错误信息: $error');
                        // 网络图片加载失败时使用本地图片
                        return Image.asset(
                          '',
                          fit: BoxFit.contain,
                        );
                      },
                      // 添加HTTP headers以确保可以访问
                      httpHeaders: {
                        'User-Agent': 'Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36',
                        'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8',
                      },
                    )
                        : Image.asset(
                      '',
                      fit: BoxFit.contain,
                    ),
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }
}
